﻿Public Class frmTimes
    Public Event ExecuteRequest(ByVal tdef As TimeDef)
    Private AllowClose As Boolean = False
    Private m_AllowShow As Boolean = False

    Public Sub Init(ByVal scheduler As AppScheduler)
        Me.Text = "G-Startup"
        Dim currow As Integer = 0
        For Each tdef As TimeDef In scheduler
            Dim pnl As New Panel
            Dim lblName As New Label()
            lblName.Name = "lblName_" & tdef.Name
            lblName.Text = tdef.Name
            lblName.TextAlign = ContentAlignment.MiddleLeft
            lblName.Width = 100
            lblName.Show()
            pnl.Controls.Add(lblName)
            lblName.Dock = DockStyle.Left
            lblName.BringToFront()

            Dim lblTime As New Label()
            lblTime.Name = "lblTime_" & tdef.Name
            lblTime.Text = CStr(tdef.TimeLeft)
            lblTime.TextAlign = ContentAlignment.MiddleLeft
            lblTime.Width = 50
            lblTime.Dock = DockStyle.Left
            lblTime.Show()
            pnl.Controls.Add(lblTime)
            lblTime.BringToFront()

            Dim btnNow As New Button
            btnNow.Name = "btnNow_" & tdef.Name
            btnNow.Tag = tdef
            btnNow.Text = "Now!"
            btnNow.Width = 60
            btnNow.Dock = DockStyle.Fill
            AddHandler btnNow.Click, AddressOf btnNow_click
            btnNow.Show()
            pnl.Controls.Add(btnNow)
            btnNow.BringToFront()
            currow += 1

            pnl.Height = 21
            pnlMain.Controls.Add(pnl)
            pnl.Dock = DockStyle.Top
            pnl.BringToFront()
        Next

        Dim btnStop As New Button()
        btnStop.Name = "btnStop"
        btnStop.Text = "Stop!"
        btnStop.Width = 210
        AddHandler btnStop.Click, AddressOf btnStop_click
        pnlMain.Controls.Add(btnStop)
        btnStop.Dock = DockStyle.Top
        btnStop.BringToFront()
        Me.ClientSize = New Size(210, btnStop.Bottom)
    End Sub
    Public Sub UpdateTimer(ByVal tdef As TimeDef)
        Dim lbl As Label = GetControl(pnlMain, "lblTime_" & tdef.Name)
        If Not lbl Is Nothing Then
            lbl.Text = CStr(tdef.TimeLeft)
        End If
    End Sub
    Public Sub SetExecuted(ByVal name As String)
        Dim btn As Button = GetControl(pnlMain, "btnNow_" & name)
        If Not btn Is Nothing Then
            btn.Enabled = False
        End If
        Dim lbl As Label = GetControl(pnlMain, "lblTime_" & name)
        If Not lbl Is Nothing Then
            lbl.Text = 0
        End If
    End Sub

    Private Function GetControl(ByVal ParentControl As Control, ByVal name As String) As Control
        If ParentControl.Name = name Then
            Return ParentControl
        End If
        For Each cnt As Control In ParentControl.Controls
            If cnt.Name = name Then
                Return cnt
            Else
                Dim found As Control = GetControl(cnt, name)
                If Not found Is Nothing Then
                    Return found
                End If
            End If
        Next
        Return Nothing
    End Function

    Private m_Destroyed As Boolean
    Public ReadOnly Property Destroyed() As Boolean
        Get
            Return m_Destroyed
        End Get
    End Property

    Private Sub btnNow_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim btn As Button = CType(sender, Button)
        Dim tdef As TimeDef = CType(btn.Tag, TimeDef)
        RaiseEvent ExecuteRequest(tdef)
        btn.Enabled = False
    End Sub
    Private Sub btnStop_click(ByVal sender As Object, ByVal e As EventArgs)
        Close()
    End Sub

    Private Sub frmTimes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If AllowClose Then
            m_Destroyed = True
            e.Cancel = False
        Else
            e.Cancel = True
            Me.Hide()
        End If
    End Sub

    Public Shadows Sub Close()
        AllowClose = True
        MyBase.Close()
    End Sub

    Private Sub frmTimes_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If Not m_allowShow Then
            Me.Visible = False
        End If
    End Sub
    Public Sub AllowShow()
        m_allowShow = True
    End Sub
End Class
